package com.wdy.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wdy.dao.RegionDao;
import com.wdy.vo.Region;
@SuppressWarnings("unchecked")
public class RegionDaoImpl extends HibernateDaoSupport implements RegionDao {

	private static final Log log = LogFactory.getLog(UserDaoHibernateImpl.class);
	
	@Override
	public List<Region> findAll() {
		// TODO Auto-generated method stub
		log.info("finding all");
		try {
			String queryString = "from Region";
			List<Region> result = (List<Region>)getHibernateTemplate().find(queryString);
			log.info("find all successful,result size: " + result.size());
			return result;
		} catch (DataAccessException e) {
			log.error("find all failed", e);
			throw e;
		}
	}

	@Override
	public Region findById(Integer id) {
		// TODO Auto-generated method stub
		log.info("finding Region instance by id");
		try {
			Region Region = (Region)getHibernateTemplate().get(Region.class, id);
			log.info("find Region instance by id successful");
			return Region;
		} catch (DataAccessException e) {
			log.error("find Region instance by id failed", e);
			throw e;
		}
	}

	@Override
	public Map<String, String> findProvince() {
		// TODO Auto-generated method stub
		String queryString = "from Region as model where model.type=1";
		Map<String, String> map = null;
		try {
			List<Region> result = (List<Region>)getHibernateTemplate().find(queryString);
			map = new HashMap<String, String>();
			for(Region region: result){
				map.put(region.getId().toString(), region.getName());
			}
		} catch (DataAccessException e) {
			log.error("find province failed", e);
			throw e;
		}
		return map;
	}

	public Map<String, String> findCity(Integer pid) {
		// TODO Auto-generated method stub
		String queryString = "from Region as model where model.type=2 and model.pid=?";
		Map<String, String> map = null;
		try {
			List<Region> result = (List<Region>)getHibernateTemplate().find(queryString, pid);
			map = new HashMap<String, String>();
			for(Region region: result){
				map.put(region.getId().toString(), region.getName());
			}
		} catch (DataAccessException e) {
			log.error("find city failed", e);
			throw e;
		}
		return map;
	}
	
	@Override
	public Map<String, String> findCounty(Integer pid) {
		// TODO Auto-generated method stub
		String queryString = "from Region as model where model.type=3 and model.pid=?";
		Map<String, String> map = null;
		try {
			List<Region> result = (List<Region>)getHibernateTemplate().find(queryString, pid);
			map = new HashMap<String, String>();
			for(Region region: result){
				map.put(region.getId().toString(), region.getName());
			}
		} catch (DataAccessException e) {
			log.error("find county failed", e);
			throw e;
		}
		return map;
	}
}
